/**
 * Copyright:Copyright(c)2014-2015
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.services.front.order.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import shop.core.ServicesManager;
import shop.services.front.cart.bean.Cart;
import shop.services.front.cart.dao.CartDao;
import shop.services.front.order.OrderService;
import shop.services.front.order.bean.Order;
import shop.services.front.order.dao.OrderDao;
import shop.services.front.orderdetail.bean.Orderdetail;
import shop.services.front.orderdetail.dao.OrderdetailDao;
import shop.services.front.seat.bean.Seat;
import shop.services.front.seat.dao.SeatDao;
import shop.services.front.spec.bean.Spec;
import shop.services.front.spec.dao.SpecDao;
import shop.services.front.spec.dao.impl.SpecDaoImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * @类名称：OrderServiceImpl
 * @创建人：Ltz
 * @创建时间：2017-09-20 下午15:48:44
 * @版本号：1.0
 * @描述：
 */
@Service("orderServiceFront")
public class OrderServiceImpl extends ServicesManager<Order, OrderDao> implements OrderService {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Resource(name = "orderDaoFront")
    @Override
    public void setDao(OrderDao orderDao) {this.dao = orderDao;}
    @Resource(name = "specDaoFront")
    private SpecDao specDao;
    @Resource(name = "cartDaoFront")
    private CartDao cartDao;
    @Resource(name = "orderdetailDaoFront")
    private OrderdetailDao orderdetailDao;
    @Resource(name = "seatDaoFront")
    private SeatDao seatDao;



    @Override
    public int insert(Order order) {
        dao.insert(order);
        //绑定座位号
        Seat seat=new Seat();
        seat.setId(order.getSeatNumber());
        seat=seatDao.selectOne(seat);
        if(seat!=null&&"in".equals(order.getType())){
            if(seat.getStatus().equals("bind")){
                order.setSeatNumber("0");
                dao.update(order);
            }else{
                seat.setStatus("bind");
                seat.setOrderID(order.getId());
                seatDao.update(seat);
            }
        }
        if("order".equals(order.getPayType())){
            String[] productIDs = order.getProductIDs().split(",");
            String[] specIDs = order.getSpecIDs().split(";");
            String[] productAmount = order.getProductAmount().split(",");
            for (int i = 0; i < productIDs.length; i++) {
                //获取规格信息
                Spec spec = new Spec();
                spec.setProductID(productIDs[i]);
                spec.setCombination(specIDs[i]);
                spec = specDao.selectOne(spec);
                //创建订单详情项
                Orderdetail orderdetail = new Orderdetail();
                orderdetail.setOrderID(order.getId());
                orderdetail.setProductID(productIDs[i]);
                orderdetail.setPrice(spec.getPrice());
                orderdetail.setNumber(Integer.valueOf(productAmount[i]));
                orderdetail.setSpecInfo(spec.getCombination());
                //计算订单详情项价格
                BigDecimal productNumber = new BigDecimal(productAmount[i]);
                productNumber.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
                BigDecimal total = spec.getPrice().multiply(productNumber);
                orderdetail.setDtotal(total);
                orderdetailDao.insert(orderdetail);//插入订单详情
                //购物车删除已购买项
                if("cart".equals(order.getSubmitType())){
                    Cart cart = new Cart();
                    cart.setUserID(order.getUserID());
                    cart.setProductID(productIDs[i]);
                    if (cartDao.selectList(cart).size() != 0) {
                        cartDao.delete(cart);
                    }
                }
            }
        }
        return Integer.parseInt(order.getId());
    }

    @Override
    public BigDecimal selectCostAmount(Order order) {
        return dao.selectCostAmount(order);
    }
}

